package com.qiangqiang.service;

import com.qiangqiang.aop.annotation.CheckHasDormitory;
import com.qiangqiang.entity.Dormitory;
import com.qiangqiang.enums.DormitoryEnum;
import com.qiangqiang.exception.DormitoryException;
import com.qiangqiang.repository.DormitoryRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.math.BigDecimal;

/**
 * Created by q on 2018/2/5.
 */
@Slf4j
@Service
public class DormitoryService {

    @Autowired
    private DormitoryRepository dormitoryRepository;

    /**
     * 添加或更新宿舍
     * @param dormitory
     * @return
     */
    public Dormitory addOrUpdateDormitory(Dormitory dormitory){

        Dormitory objDormitory=dormitoryRepository.findOne(dormitory.getDId());
        if(objDormitory==null){
           //查不到该宿舍，证明是新宿舍，所以添加到表中
            //TODO 默认值
            return dormitoryRepository.save(dormitory);
        }
        //如果查到，就更新宿舍信息
        BeanUtils.copyProperties(dormitory,objDormitory);
        return dormitoryRepository.save(objDormitory);
    }
    /**
     *  根据宿舍号 删除一个宿舍
     *
     * @param id
     */
    @Transactional(rollbackFor = Exception.class)
    @CheckHasDormitory
    public void deleteDormitory(String id){
        dormitoryRepository.deleteByDId(id);
    }

    /**
     * 查询该宿舍总得分情况
     * @param id
     * @return
     */
    @CheckHasDormitory
    public BigDecimal queryScore(String id){
            return dormitoryRepository.queryScore(id);
    }

    /**
     * 查看该宿舍得分减分详细情况
     */

}
